<?php

namespace app\api\model\xykeep\user;

use think\Model;
use traits\model\SoftDelete;
use app\api\model\xykeep\User;
use app\common\model\Prison as prisonModel;

class Taker extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'xykeep_user_taker';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];
    

    /**
     * 列表
     */
    public static function getList($params)
    {
        extract($params);
        $user = User::info();
        $list = self::where(['user_id'=>$user->id])->order('id desc')->paginate();
        foreach ($list as $k=>&$v){
            $prison = prisonModel::where(['id'=>$v['prison_id']])->find();
            $v['prison_name'] = $prison['name'];
            $v['postcode_str'] = $prison['postcode'];
            $v['postcode'] = str_split($prison['postcode']);
        }
        return $list;
    }
    

    /**
     * 添加
     */
    public static function add($params){

        extract($params);
       
        $user = User::info();

        if(isset($id) && $id > 0){
            $accont = self::get($id);
        }else{
            $accont = new self();
        }

        $accountData['user_id'] = $user->id;
        $accountData['consignee'] = $params['consignee'];
        $accountData['gay_code'] = $params['gay_code'];
        $accountData['prison_id'] = $params['prison_id'];

        return $accont->allowField(true)->save($accountData);

    }

    /**
     * 详情
     */
    public static function getDetail($id)
    {
        $user = User::info();
        $info = self::where(['id'=>$id,'user_id'=>$user->id])->find();
        $info['prison_name'] = prisonModel::where(['id'=>$info['prison_id']])->value('name');
        $prison_data = prisonModel::where([
            'id' => $info['prison_id'],
        ])->find();
        $info['super_com'] = $prison_data['is_super_com'];
        $info['super_special'] = $prison_data['is_super_special'];
        return $info;
    }

    /**
     * 删除
     */
    public static function del($id)
    {
        $user = User::info();
        return self::get(['id' => $id, 'user_id' => $user->id])->delete();
    }





}
